/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.PanelUI;

/**
 *
 * @author paulocanedo
 */
public class InfoPanel {

    private GridBagConstraints gridBagConstraints;
    private int elements = -1;
    private JPanel component = new JPanel();
    private JButton closeButton;
    private JButton originalDefaultButton;

    public InfoPanel() {
        this(null);
    }

    public InfoPanel(final JRootPane rootPane) {
        GridBagLayout gridBagLayout = new GridBagLayout();
        component.setLayout(gridBagLayout);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = ++elements;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;

        closeButton = new JButton("Fechar");
        closeButton.addActionListener(new ActionListener()      {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (rootPane != null) {
                    rootPane.setDefaultButton(originalDefaultButton);
                }
                component.setVisible(false);
            }
        });
        component.add(closeButton, gridBagConstraints);

        if (rootPane != null) {
            originalDefaultButton = rootPane.getDefaultButton();
            rootPane.setDefaultButton(closeButton);
        }
    }

    public void putLine(String label, Object content) {
        JLabel jlabel = new JLabel(label + ": ");
        jlabel.setHorizontalAlignment(JLabel.RIGHT);
        jlabel.setFont(jlabel.getFont().deriveFont(Font.BOLD));
        jlabel.setForeground(Color.GRAY);
        JTextField textField = new JTextField(content == null ? "" : content.toString());
        textField.setEditable(false);
        textField.setBorder(new EmptyBorder(2, 2, 2, 2));
        textField.setFont(textField.getFont().deriveFont(Font.PLAIN));

        textField.setMinimumSize(new Dimension(400, 22));
        textField.setSize(new Dimension(400, 22));
        textField.setPreferredSize(new Dimension(400, 22));
        textField.setOpaque(false);
        textField.putClientProperty("borderFocus", Boolean.FALSE);
        textField.putClientProperty("opaque", Boolean.FALSE);
        textField.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
        
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = ++elements;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        component.add(jlabel, gridBagConstraints);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = elements;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        component.add(textField, gridBagConstraints);
    }

    public void setUI(PanelUI panelUI) {
        component.setUI(panelUI);
    }

    public JComponent getComponent() {
        return component;
    }

    public JButton getCloseButton() {
        return closeButton;
    }

    public static void main(String... args) {
        JFrame frame = new JFrame();
        InfoPanel panel = new InfoPanel();
        panel.setUI(new PanelUI()    {

            @Override
            public void paint(Graphics g, JComponent c) {
//                super.paint(g, c);
//                g.setColor(Color.red);
//                g.fillRect(0, 0, c.getWidth(), c.getHeight());
            }
        });

        panel.putLine("teste", "Valor do teste");
        panel.putLine("Nome", "Fulano");
        panel.putLine("Sobrenome", "da Silva");
        panel.putLine("Endereco", "Rua bolinha das dores, numero 9.987");
        frame.setContentPane(panel.getComponent());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0, 0, 600, 380);
        frame.setVisible(true);
    }
}
